package org.example.controller;

import lombok.AllArgsConstructor;
import org.example.dao.Result;
import org.example.dao.User;
import org.example.enums.responseEnum;
import org.example.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户登录，注册，获取用户信息，修改信息等操作
 */
@AllArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @PostMapping("/login")
    Result login(@RequestBody User user) {
        responseEnum login = userService.login(user);
        if (login == responseEnum.success) {
            return new Result(200, "登录成功", userService.getUser(user));
        } else if (login == responseEnum.error) {
            return new Result(401, "密码错误", null);
        } else {
            return new Result(404, "用户不存在", null);
        }
    }

    @PostMapping("/register")
    Result register(@RequestBody User user) {
        responseEnum register = userService.register(user);
        if (register == responseEnum.success) {
            return new Result(200, "注册成功", null);
        } else if (register == responseEnum.error) {
            return new Result(400, "用户名或密码格式错误", null);
        } else {
            return new Result(401, "用户名已存在", null);
        }
    }

    //自动登录时可以获取用户信息
    @PostMapping("/getUser")
    Result getUser(@RequestBody User user) {
        return new Result(200, "获取成功", userService.getUser(user));
    }
    @PostMapping("/getUserById")
    Result getUserByID(@RequestBody User user) {
        return new Result(200, "获取成功", userService.getUserById(user));
    }




    @PostMapping("/updateUser")
    Result updateUser(@RequestBody User user) {
        responseEnum res = userService.updateUser(user);
        if (res == responseEnum.success) {
            return new Result(200, "修改成功",null);
        } else if (res == responseEnum.error) {
            return new Result(500, "该用户名已经存在", null);
        } else {
            return new Result(400, "请传入id", null);
        }
    }
}
